package componentesAux;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class LectorIpFile {
	private static final String INI_FILE = "conf.ini";
	private static final String PATRON = "ip: ";
	
	public static void main(String[] args){
		escribirIP("192.168.0.1");
		existeIniFile();
		System.out.println(leerIP());
		/*
		// ------- ESCRIBIR -------
		try{
			FileWriter file = new FileWriter(INI_FILE,false);
			BufferedWriter out = new BufferedWriter(file);
			out.write(PATRON+"127.0.0.1");
			out.newLine();
			out.flush();
			out.close();
			file.close();
			
			}catch(Exception e){
				
			}
		// ------- COMPROBAR SI EXISTE -------
		File f = new File(INI_FILE);
		if(f.isFile()){
			System.out.println("El archivo existe");
		}else{
			System.out.println("El NO archivo existe");
		}
		// ------- LEER -------
		try {
			FileReader reader = new FileReader(INI_FILE);
			BufferedReader input = new BufferedReader(reader);
			String linea = input.readLine();
			if(linea!=null){
				String[] desmembrado = linea.split(PATRON);
				String ip = desmembrado[1];
				System.out.println(ip);
			}
			input.close();
			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
	}
	
	public static boolean escribirIP(String ip){
		try{
			System.out.println("INTENTO ESCRIBIR IP");
			File f = new File(INI_FILE);
			System.out.println("INTENTO ESCRIBIR IP 2");
			//new Fil
			if(!f.exists()){
				if(f.createNewFile()){
					System.out.println("Creo el archivo conf correctamente");
				}else{
					System.out.println("NO pudo crear el archivo conf!!!!");
				}
			}
		
			FileWriter file = new FileWriter(INI_FILE,false);
			BufferedWriter out = new BufferedWriter(file);
			out.write(PATRON+ip);
			out.newLine();
			out.flush();
			out.close();
			file.close();
			return true;
			
			}catch(Exception e){
				//e.printStackTrace();
				System.err.println(e.getMessage());
				return false;
			}
	}
	public static String leerIP(){
		try {
			FileReader reader = new FileReader(INI_FILE);
			BufferedReader input = new BufferedReader(reader);
			String linea = input.readLine();
			String ip=null;
			if(linea!=null){
				String[] desmembrado = linea.split(PATRON);
				ip = desmembrado[1];
				//System.out.println(ip);
			}
			input.close();
			reader.close();
			return ip;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static boolean existeIniFile(){
		File f = new File(INI_FILE);
		if(f.isFile()){
			System.out.println("El archivo existe");
			return true;
		}else{
			System.out.println("El NO archivo existe");
			return false;
		}
	}
}
